/**
 * 用户升级
 * @author yanwl
 * @date 2014-11-10
 */
$(function(){
	//升级去支付
	$('.upgrade').on('click',function(){
		var userNum = $(".dropbox_value").attr("data-value"),
			orderId = $('#orderId').val();
		location.href = "/myservice/order/toUpgradePayPage?orderId="+orderId+"&userNum="+userNum;
	});
	
	//下拉框样式
	$(".dropbox").dropBox({
	   width: 100,         //控件宽度,默认单位px
	   height: 20,         //控件高度,默认单位px
	   sltheight: 100      //下拉框最大高度,超出则出现滚动条,默认单位px
	});
});

//下拉框改变时计算价格
$(document).on('select_loaded',function(){
	/*价格计算*/
	$('.dropbox').find('li').on('click',function(){
		var $parent = $(this).parents('.dropbox').prev(),
			baseUser = $('#baseUser').val(),
			accountNum = $(this).attr('data-value'),
			userSpace = accountNum - baseUser,
			goodsPrice = $('#goodsPrice').val(),
			useLife = $('#useLife').val(),
			overTime = new Date( $('#overTime').val().replace(/-/gi,"/") ),
			feeFormula = $.trim($('#feeFormula').val()),
			feeBasenum = $.trim($('#feeBasenum').val()),
			freeOverTime = $.trim($('#freeOverTime').val());
		
		
		var startTime = new Date(getNowDate());
		if(freeOverTime != null && freeOverTime != '') {
			freeOverTime = new Date( freeOverTime.replace(/-/gi,"/") );
			startTime = freeOverTime;
		}
		
		var month = calculateMonth( startTime , overTime , "returnMonth" );
		//永久产品
		if(useLife == -1) {
			useLife = 1; 
		}else {
			useLife = 12; 
		}
		var price = parseFloat(calculateByFee(feeFormula,feeBasenum,userSpace,month,goodsPrice)).toFixed(2);
    	$('.need2pay em').text(price);
	});
	$('.dropbox').find('li:first').triggerHandler("click");
})


//获取服务器当前时间
function getNowDate() {
	var time = '';
	$.ajax({
		type : "post",
		url : "/common/getNowDate",
		async : false,
		dataType : "json",
		success : function(data){
			time = toDate(data.time,"yyyy-MM-dd");
		},
	    error : function() {
	    	BSTYUN.dialog.warn("网络异常，请重试");
			return;
	    }
	})
	return time;
}
